#include "vsprintf.h"
#include "string.h"
#include "printf.h"
#include "serial.h"
#include "interrupts.h"
#include "timer.h"






#define	OUTBUFSIZE	1024
#define	INBUFSIZE	1024


static unsigned char g_pcOutBuf[OUTBUFSIZE];
static unsigned char g_pcInBuf[INBUFSIZE];


int myprintf(const char *fmt, ...)
{
	int i;
	int len;
	va_list args;

	va_start(args, fmt);
	len = vsprintf((char *)g_pcOutBuf,fmt,args);
	va_end(args);
	for (i = 0; i < strlen((char *)g_pcOutBuf); i++)
	{
		Uart_SendByte(g_pcOutBuf[i]);
	}
	return len;
}



int scanf(const char * fmt, ...)
{
	int i = 0;
	unsigned char c;
	va_list args;
#if 0	
	while(1)
	{
		c = Uart_Getc();
		
		if((c == 0x0d) || (c == 0x0a))
		{
			g_pcInBuf[i] = '\0';
			break;
		}
		else
		{
			g_pcInBuf[i++] = c;
		}
	}
#endif	
	unsigned char *string = g_pcInBuf;
	unsigned char *string2 = string;
	while ((c = Uart_Getch())!='\r')
	{
	 	if (c == '\b') 
		{
			 if ((int)string2 < (int)string) 
			 {
				 Uart_SendString("\b \b");
				 string--;
			 }
	 	} 
		else 
		{
			 *string++ = c;
			 Uart_SendByte(c);
	 	}
	}
	*string='\0';
	Uart_SendByte('\n');

	va_start(args,fmt);
	i = vsscanf((char *)g_pcInBuf,fmt,args);
	va_end(args);

	return i;
}
int scanfs(const char * fmt, ...)
{
	int i = 0;
	unsigned char c;
	va_list args;
	
	unsigned char *string = g_pcInBuf;
	unsigned char *string2 = string;
	while ((c = Uart_Getch())!='\r')
	{
	 	if (c == '\b') 
		{
			 if ((int)string2 < (int)string) 
			 {
				 Uart_SendString("\b \b");
				 string--;
			 }
	 	} 
		else 
		{
			 *string++ = c;
			 Uart_SendByte('*');
	 	}
	}
	*string='\0';
	Uart_SendByte('\n');
	
	va_start(args,fmt);
	i = vsscanf((char *)g_pcInBuf,fmt,args);
	va_end(args);

	return i;
}


